package com.situ.day4;

//共同特征
public class Student {
    //属性 field
    private int id;
    private String name;
    private int age;
    private String gender;

    //如果没有写任何构造方法，Java默认提供一个无参构造方法
    //如果你写了任何一个构造方法，Java就不再提供默认构造方法
    public Student() {
        System.out.println("Student.Student");
    }

    //有参构造方法
    public Student(int id, String name, int age, String gender) {
        this.id = id;
        this.name = name;
        this.age = age;
        this.gender = gender;
    }

    public int getId() {
        return id;
    }

    //定义方法
    //方法签名         参数
    public void setId(int id) {//方法体
        this.id = id;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getGender() {
        return gender;
    }

    public void setGender(String gender) {
        this.gender = gender;
    }

    //public的get和set方法间接访问私有的属性
    public void setAge(int age) {
        //Variable 'age' is assigned to itself
        //就近原则
        //age = age;
        //this:代表当前类对象
        if (age < 0 || age > 125) {
            //抛出异常
            return;
        }
        this.age = age;
    }

    public int getAge() {
        return age;
    }

    @Override
    public String toString() {
        return "Student{" +
                "id=" + id +
                ", name='" + name + '\'' +
                ", age=" + age +
                ", gender='" + gender + '\'' +
                '}';
    }
}
